package com.firesrc.web;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.firesrc.bean.MailContent;
import com.firesrc.service.MailContentService;
import com.firesrc.service.impl.MailContentServiceImpl;

/**
 * Servlet implementation class MailContentServlet
 */
public class MailContentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MailContentServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		String type = request.getParameter("type");
		MailContentService service = new MailContentServiceImpl();
		if ("add".equals(type)) {
			MailContent content = new MailContent();
			content.setId(UUID.randomUUID().toString());
			content.setKind(request.getParameter("kind"));
			content.setName(request.getParameter("name"));
			content.setText(request.getParameter("text"));
			content.setState(request.getParameter("state"));
			service.add(content);
		}
		List<MailContent> allList = service.getAllList();
		request.setAttribute("list", allList);
		request.getRequestDispatcher("mailContentList.jsp").forward(request, response);
		response.getWriter().print("success");
	}
}
